﻿
/**
* Var 0.1.12
* @author Administrator
*/
if (Masque && !Masque.C) {
    Masque.C = {
        Operate: {
            False: 0, //向上
            Once: 1, //向右
            Unbound: 2,
            Default: 2
        },

        SubmitButton: function(button) {
            input = window.event.srcElement;
            if (event.keyCode == 13) {
                btn = $(button);
                if (btn != null) {

                    if (btn.type && (btn.type == "text" || btn.type == "password")) {
                        btn.focus();
                    }
                    else {
                        btn.focus();
                        btn.click();
                    }
                    event.returnValue = false;
                    return false;
                }
            }
            return true;

        },
        // For IE.  Go through predefined tags and disable tabbing into them.
        disableTabIndexes: function() {
            if (document.all) {
                var i = 0;
                for (var j = 0; j < gTabbableTags.length; j++) {
                    var tagElements = document.getElementsByTagName(gTabbableTags[j]);
                    for (var k = 0; k < tagElements.length; k++) {
                        gTabIndexes[i] = tagElements[k].tabIndex;
                        tagElements[k].tabIndex = "-1";
                        i++;
                    }
                }
            }
        },

        // For IE. Restore tab-indexes.
        restoreTabIndexes: function() {
            if (document.all) {
                var i = 0;
                for (var j = 0; j < gTabbableTags.length; j++) {
                    var tagElements = document.getElementsByTagName(gTabbableTags[j]);
                    for (var k = 0; k < tagElements.length; k++) {
                        tagElements[k].tabIndex = gTabIndexes[i];
                        tagElements[k].tabEnabled = true;
                        i++;
                    }
                }
            }
        },
        RedioBoxListSetVisible: function() {
            if (arguments.length > 2) {
                var rbl = document.getElementsByName(arguments[0]);

                for (var i = 1; i < arguments.length; i += 2) {
                    if (arguments[i] && arguments[i + 1]) {
                        var obj = document.getElementById(arguments[i + 1]);
                        if (obj) {
                            if (rbl[arguments[i]].checked) {
                                //obj.style.visibility = "visible"; 
                                obj.style.display = "";
                            }
                            else {
                                //obj.style.visibility = "visible"; 
                                obj.style.display = "none";
                            }
                        }
                    }
                }
            }
        },
        QuickSubmit: function(obj, btn) {
            obj = $(obj);
            btn = $(btn);
            if (obj == null || btn == null)
                return;
            obj.onkeydown = function(e) {

                if (Masque.isIE) // window.eventIE
                {
                    if (event.ctrlKey && GetKeyCode(event) == 13) {
                        btn.click();
                    }
                }
                else {
                    if (e.ctrlKey && GetKeyCode(e) == 13) {
                        btn.click();
                    }
                }
            }
        },
        CopyText: function(_sTxt) {
            if (Masque.isIE) {
                clipboardData.setData('Text', _sTxt);
                alert("网址“" + _sTxt + "”\n已经复制到您的剪贴板中\n您可以使用Ctrl+V快捷键粘贴到需要的地方");
            } else {
                prompt("请复制网站地址:", _sTxt);
            }
        },
        TextBoxSelect: function(oTextbox, iStart, iEnd) {
            oTextbox = $(oTextbox);
            if (oTextbox == null) return;
            switch (arguments.length) {
                case 1:
                    oTextbox.select();
                    break;
                case 2:
                    iEnd = oTextbox.value.length;
                    /* falls through 由case3继续处理*/
                case 3:
                    if (isIE) {
                        var oRange = oTextbox.createTextRange();
                        oRange.moveStart("character", iStart);
                        oRange.moveEnd("character", -oTextbox.value.length + iEnd);
                        oRange.select();
                    } else {
                        oTextbox.setSelectionRange(iStart, iEnd);
                    }
            }
        },
        MoveCursorToEnd: function(_oTextbox) {
            o = $(_oTextbox);
            if (o == null) return;
            o.focus();
            if (Masque.isIE) {
                var rt = o.createTextRange();
                rt.collapse(true);     //光标移至为最后;
                rt.moveStart("character", o.value.length);   //从m位开始
                rt.select();     //光标放置最前
            }
            else {
                o.setSelectionRange(o.value.length, o.value.length);
            }
        },
        RedioBoxList_SelectThis: function(obj) {
            obj = $(obj);
            if (typeof (obj) == "object" && (obj.type == "radio" || obj.type == "checkbox") && obj.checked == true) {
                var allInputElements = document.getElementsByTagName("input");

                for (var i = 0; i < allInputElements.length; i++) {

                    var inputObj = allInputElements[i];
                    if (inputObj != null && inputObj != obj && inputObj.type == obj.type && inputObj.group == obj.group && inputObj.checked == true) {
                        inputObj.checked = false;
                    }
                }
            }
        },
        CursorPosition: function(textBox) {
            var start = 0, end = 0;
            //如果是Firefox(1.5)的话，方法很简单
            if (typeof (textBox.selectionStart) == "number") {
                start = textBox.selectionStart;
                end = textBox.selectionEnd;
            }
            //下面是IE(6.0)的方法，麻烦得很，还要计算上'\n'
            else if (document.selection) {
                var range = document.selection.createRange();
                if (range.parentElement().id == textBox.id) {
                    // create a selection of the whole textarea
                    var range_all = document.body.createTextRange();
                    range_all.moveToElementText(textBox);
                    //两个range，一个是已经选择的text(range)，一个是整个textarea(range_all)
                    //range_all.compareEndPoints()比较两个端点，如果range_all比range更往左(further to the left)，则 
                    //返回小于0的值，则range_all往右移一点，直到两个range的start相同。
                    // calculate selection start point by moving beginning of range_all to beginning of range
                    for (start = 0; range_all.compareEndPoints("StartToStart", range) < 0; start++)
                        range_all.moveStart('character', 1);
                    // get number of line breaks from textarea start to selection start and add them to start
                    // 计算一下\n
                    for (var i = 0; i <= start; i++) {
                        if (textBox.value.charAt(i) == '\n')
                            start++;
                    }
                    // create a selection of the whole textarea
                    var range_all = document.body.createTextRange();
                    range_all.moveToElementText(textBox);
                    // calculate selection end point by moving beginning of range_all to end of range
                    for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end++) {
                        range_all.moveStart('character', 1);
                    }
                    // get number of line breaks from textarea start to selection end and add them to end
                    for (var i = 0; i <= end; i++) {
                        if (textBox.value.charAt(i) == '\n')
                            end++;
                    }
                }
            }
            //return [start, end]; // 包括选中区域的起始位置
            // modified to return as Object
            return { "start": start, "end": end, "item": [start, end] };
        }


    };
    //    Masque.C.PanelSelector = Class();
    //    Masque.C.PanelSelector.extend ({
    //        initialize : function(panelBox,options) {
    //            var me = this;
    //            this.PanelBox = $(labelBox);
    //            this.SelectPanel = null;
    //            this.SelectIndex = 0;
    //            this.Panels = [];
    //            if(this.PanelBox)
    //            {
    //                this.Panels = document.getElementsByClassName(this.HiddenClass,this.LabelBox);
    //                this.Select(0);
    //            }
    //            addEvent(this.LabelBox, "click", function(oEvent){me._SelectEvent(oEvent)});
    //            this.SetOption(options);
    //        },
    //        AddPanel : function(panels){
    //            var index = arguments[1];
    //            if(index == null || index < 0)
    //                index = -1;
    //            else if(index >
    //                 
    //            if(panel            
    //        }
    //        ,
    //        _SelectEvent:function(oEvent)
    //        {
    //            if(Masque.isIE){ oEvent.cancelBubble = true; } else { oEvent.stopPropagation(); }
    //            this._Select(oEvent.srcElement);
    //            
    //        },
    //        _Select:function(label){
    //            if(label == null)
    //                return;
    //            else
    //            {
    //                for(var i = 0 ; i < this.Labels.length ; i ++)
    //                {
    //                    var l = this.Labels[i];
    //                    if(l == label)
    //                    {
    //                        this.SelectIndex = i;
    //                        this.SelectLabel = l;
    //                        if(l.className == this.HiddenClass)
    //                        {
    //                            l.className = this.DisplayClass;
    //                            var p = $(l.getAttribute("panel"));
    //                            if(p != null)
    //                            {
    //                                p.style.display = "";
    //                            }
    //                        }
    //                    }
    //                    else
    //                    {
    //                        if(l.className == this.DisplayClass)
    //                        {
    //                            l.className = this.HiddenClass;
    //                            var p = $(l.getAttribute("panel"));
    //                            if(p != null)
    //                            {
    //                                p.style.display = "none";
    //                            }
    //                        }

    //                    }
    //                }
    //            }
    //        },
    //        Select:function(index){
    //            if(this.Labels.Length == 0)
    //                return;
    //                
    //            if(index < 0)
    //                index  = 0;
    //             else if(index >= this.Labels.Length)
    //                index = this.Labels.Length - 1;
    //                
    //            this._Select(this.Labels[index]);
    //            
    //        },
    //        SelectNext:function(){
    //            this.Select(this.SelectIndex+1);
    //        },
    //        SelectPrve:function(){
    //            this.Select(this.SelectIndex-1);
    //        },
    //        option : {
    //        }
    //        
    //        
    //    });
    Masque.C.LabelSelector = Class();
    Masque.C.LabelSelector.extend({
        initialize: function(labelBox, hiddenClass, displayClass, options) {
            var me = this;
            this.option = {};
            this.LabelBox = $(labelBox);
            this.HiddenClass = hiddenClass;
            this.DisplayClass = displayClass;
            this.SelectLabel = null;
            this.SelectIndex = 0;
            this.Labels = [];
            if (this.LabelBox) {
                this.Labels = document.getElementsByClassName(this.HiddenClass, this.LabelBox);
                this.Select(0);
            }
            this.SetOption(options);
            if (this.option.Event != "mouseover")
                this.option.Event = "click";
            addEvent(this.LabelBox, this.option.Event, function() { me._SelectEvent() });
        },
        _SelectEvent: function() {
            var oEvent = event;

            //if (Masque.isIE) { oEvent.cancelBubble = true; } else { oEvent.stopPropagation(); }
            obj = oEvent.srcElement;
            if (obj.className == this.HiddenClass) {
                this._Select(obj);
            }
        },
        _Select: function(label) {
            if (label == null)
                return;
            else {
                for (var i = 0; i < this.Labels.length; i++) {
                    var l = this.Labels[i];
                    if (l == label) {
                        this.SelectIndex = i;

                        if (this.option.OnSelectIndexChange != null) {
                            this.option.OnSelectIndexChange(this.SelectIndex, this.Labels.length);
                        }

                        this.SelectLabel = l;
                        if (l.className == this.HiddenClass) {
                            l.className = this.DisplayClass;
                            var p = $(l.getAttribute("panel"));
                            if (p != null) {
                                p.style.display = "";
                            }
                        }
                    }
                    else {
                        if (l.className == this.DisplayClass) {
                            l.className = this.HiddenClass;
                            var p = $(l.getAttribute("panel"));
                            if (p != null) {
                                p.style.display = "none";
                            }
                        }

                    }
                }
            }
        },
        Select: function(index) {
            if (this.Labels.Length == 0)
                return;

            if (index < 0) {
                if (this.SelectLabel != null) {
                    if (this.SelectLabel.className == this.DisplayClass) {
                        this.SelectLabel.className = this.HiddenClass;
                        var p = $(this.SelectLabel.getAttribute("panel"));
                        if (p != null) {
                            p.style.display = "none";
                        }
                    }
                }
                return;
            }
            else if (index >= this.Labels.Length) {
                index = this.Labels.Length - 1;
            }
            this._Select(this.Labels[index]);

        },
        SelectNext: function() {
            this.Select(this.SelectIndex + 1);
        },
        SelectPrve: function() {
            this.Select(this.SelectIndex - 1);
        },
        option: {}
    });

    Masque.C.MouseMovePreview = Class();
    Masque.C.MouseMovePreview.extend({
        initialize: function(previewBox, options) {
            var me = this;
            this.option = {};
            this.SetOption(options);
            this.option.PreviewBox = $(previewBox);
            this.PreviewImage = this.option.PreviewBox.getElementsByTagName("IMG")[0];
            if (this.option.PreviewBox) {
                this.option.PreviewBox.style.display = "none";
            }
        },
        Show: function(image) {

            var imageObj = null;
            var imageSrc = "";
            var position = "";
            if (arguments.length == 3) {
                imageObj = image;
                imageSrc = arguments[1];
                position = arguments[2];
            }
            else {
                imageObj = image;
                if (image && image.src) {
                    imageSrc = image.src;
                }
                position = argument[1];

            }
            if (this.PreviewImage) {
                this.PreviewImage.src = imageSrc;
            }
            this.option.PreviewBox.style.display = "";

            for (var p in this.option.PreviewBoxControl) {
                if (p == position) {
                    var t = this.option.PreviewBoxControl[position];
                    for (var s in t) {
                        eval("this.option.PreviewBox.style." + s + "= t[s];");
                    }
                    break;
                }
            }
            var me = this;
            if (Masque.V.isElement(imageObj) && imageObj.BindMouseMovePreview != this) {
                addEvent(imageObj, "click", function() { me._ClickEvent() });
                addEvent(imageObj, "mouseout", function() { me._Hidden() });
                imageObj.BindMouseMovePreview = this;
            }


        },
        _Hidden: function() {
            this.option.PreviewBox.style.display = "none";
        },
        _ClickEvent: function() {
            var oEvent = event;
            if (Masque.isIE) { oEvent.cancelBubble = true; } else { oEvent.stopPropagation(); }
            if (this.option.OnClick) {
                this.option.OnClick(oEvent.srcElement);
            }

        },
        option: {
            PreviewBox: null,
            PreviewBoxControl: { Default: {} },
            OnClick: null
        }

    });

}